package com.zpark;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @PackageName: com.zpark
 * @ClassName: JsoupDemo03
 * @Description: node节点对象
 * @author: RZS
 * @date: 2021/8/13  22:48
 */
public class JsoupDemo03 {
    public static void main(String[] args) throws Exception {
        // 获取xml文件路径
        String path = JsoupDemo03.class.getClassLoader().getResource("users.xml").getPath();
        // 由于路径中含有中文，所以需要进行解码处理
        path = URLDecoder.decode(path, "utf-8");
        System.out.println(path);

        // 获取document对象
        Document document = Jsoup.parse(new File(path), "utf-8");

        // 查询name标签
        Elements elements = document.select("name");
        System.out.println(elements);
        System.out.println("---------------------------");
        // 查询id值为Z的元素
        Elements elements1 = document.select("#Z");
        System.out.println(elements1);
        System.out.println("---------------------------");

        // 获取user标签，并且number属性为zpark-001标签
        Elements elements2 = document.select("user[number='zpark-001']");
        System.out.println(elements2);
        System.out.println("---------------------------");

        // 获取user标签，并且number属性为zpark-001的gender子标签
        Elements elements3 = document.select("user[number='zpark-001']>age");
        System.out.println(elements3);
        System.out.println("---------------------------");
    }
}
